package br.ucb.cm.config.wizard;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import javax.swing.JOptionPane;

import br.ucb.cm.config.bean.Config;
import br.ucb.cm.config.page.IfcPage;
import br.ucb.cm.config.page.board.BoardPage;
import br.ucb.cm.config.page.integration.IntegrationPage;
import br.ucb.cm.config.page.piece.PiecePage;
import br.ucb.cm.config.page.save.SavePage;
import br.ucb.cm.config.wizard.ui.ConfigWizardUI;
import br.ucb.cm.config.wizard.ui.IfcConfigWizard;

public class ConfigWizard implements IfcConfigWizard{
	private List<IfcPage> pages;
	private int pageIndex;
	private ConfigWizardUI ui;
	private Config config;

	public ConfigWizard() {
		this.pages = new ArrayList<IfcPage>();
		this.pageIndex = 0;
		this.ui = new ConfigWizardUI(this);
		this.config = new Config();
		
		this.addPagina(new BoardPage(), new PiecePage(), new IntegrationPage(getConfig()), new SavePage(getConfig()));
	}

	public void addPagina(IfcPage page) {
		getPages().add(page);
	}
	public void addPagina(IfcPage ... pages) {
		getPages().addAll(Arrays.asList(pages));
	}

	public void show() {
		if(refresh())
			getUi().setVisible(true);
	}

	private boolean refresh() {
		if (getPages().size() > 0) {
			refreshButtons();
			refreshPage();
			return true;
		}
		return false;
	}

	private void refreshPage() {
		IfcPage currentPage = getCurrentPage();
		currentPage.call();
		
		getUi().setPageTitle(currentPage.getTitle());
		getUi().setPageSubtitle(currentPage.getSubTitle());
		getUi().setIndex(getPageIndex(), getPages().size());
		getUi().refreshPage(currentPage.getPage());
	}
	private void refreshButtons() {
		if(isFirstPage())
			getUi().refreshBackButton(StateEnum.START);
		else
			getUi().refreshBackButton(StateEnum.MID);

		if(isLastPage())
			getUi().refreshNextButton(StateEnum.END);
		else 
			getUi().refreshNextButton(StateEnum.MID);
	}
	public List<IfcPage> getPages() {
		return pages;
	}
	public void setPages(List<IfcPage> pages) {
		this.pages = pages;
	}
	public int getPageIndex() {
		return pageIndex;
	}
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}
	public ConfigWizardUI getUi() {
		return ui;
	}
	public void setUi(ConfigWizardUI ui) {
		this.ui = ui;
	}

	@Override
	public void doAction(ActionEnum action) {
		switch (action) {
		case NEXT:
			doNext();
			break;
		case PREV:
			doPrev();
			break;
		}

		refresh();
	}
	private void doPrev() {
		if(!isFirstPage())
			setPageIndex(getPageIndex() - 1);
	}
	private void doNext() {
		if(!isCurrentPageValid()){
			return;
		}

		updateConfig();
		
		if (!isLastPage())
			setPageIndex(getPageIndex() + 1);
		else
			//Corrigir dps
			getUi().dispose();
	}

	private void updateConfig() {
		List<Object> configList = new Vector<Object>();
		
		for (int i = 0; i <= getPageIndex(); i++)
			configList.add(getPages().get(i).getValue());
		
		Config.updateConfig(configList, getConfig());
		System.gc();
	}

	private boolean isCurrentPageValid(){
		String error = getCurrentPage().validate();

		if(error != null){
			//Corrigir dps
			JOptionPane.showMessageDialog(null, error, "Erro", JOptionPane.ERROR_MESSAGE, null);
			return false;
		}
		
		return true;
	}

	private boolean isLastPage() {
		return getPageIndex() == getPages().size() - 1;
	}

	private boolean isFirstPage() {
		return getPageIndex() == 0;
	}

	private IfcPage getCurrentPage() {
		return getPages().get(getPageIndex());
	}

	public Config getConfig() {
		return config;
	}

	public void setConfig(Config config) {
		this.config = config;
	}
	
}

